package com.cuixichang.minimalism.acl;

import cn.hutool.core.util.StrUtil;
import com.cuixichang.minimalism.basal.base.Validate;
import com.cuixichang.minimalism.basal.core.RelationDict;
import com.cuixichang.minimalism.basal.enums.SubassemblyTypeEnum;
import com.cuixichang.minimalism.service.IFormPlugService;
import com.cuixichang.minimalism.transfer.dto.IFormPlugDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public abstract class SubassemblyDesignAbs{
    @Autowired
    protected IFormPlugService iFormPlugService;

    /**
     * 表单字段类型
     * @return
     */
    public abstract SubassemblyTypeEnum getType();

    /**
     * 限制条件的示例数据
     * @return
     */
    public String example(){
        return StrUtil.EMPTY;
    }

    /**
     * 创建表单字段规则验证限制结构方法
     * @param structure
     * @return
     */
    public abstract Validate structureValidate(String structure);

    /**
     * 用户数据存储验证数据方法
     * @param structure
     * @param value
     * @return
     */
    public abstract Validate valueValidate(String structure, String value);

    /**
     * 存储值在预览时候进行转换的方法
     * @param structure
     * @param value
     * @return
     */
    public abstract String previewValue(String structure, Object value);

    /**
     * 数据库数据加载时进行转换的方法
     * @param value
     * @return
     */
    public abstract Object resultValue(String value);

    /**
     * 传参预存数据库进行数据转换方法
     * @param value
     * @return
     */
    public abstract String resultValue(Object value);

    public List<RelationDict> getApplicationPlug(String systemCode,String moduleName,String fieldType){
       return getApplicationBindingPlug(systemCode,moduleName,fieldType,null);
    }

    public List<RelationDict> getApplicationBindingPlug(String systemCode,String moduleName,String fieldType,Long formId){
        Assert.hasText(systemCode, "系统编码不能为空");
        Assert.hasText(moduleName, "模块名称不能为空");
        Assert.hasText(fieldType, "字段类型不能为空");
        List<IFormPlugDTO> iFormPlugs = iFormPlugService.selectList(IFormPlugDTO.builder().createSystemCode(systemCode)
                      .moduleName(moduleName).fieldType(fieldType).isEnable(true).build());
        return getApplicationBindingPlug(iFormPlugs,formId);
    }

     public List<RelationDict> getApplicationBindingPlug(List<IFormPlugDTO> iFormPlugs,Long formId){
        List<PlugDesign> plugDesign = PlugDesignComponent.getPlugDesign(this.getType());
        if(CollectionUtils.isEmpty(plugDesign)) return Collections.emptyList();
        return plugDesign.stream().filter(plug->{
          if(plug.exclusive() && !CollectionUtils.isEmpty(iFormPlugs)){
              Optional<IFormPlugDTO> optionalIFormPlug = iFormPlugs.stream().filter(single -> plug.plugName()
                      .equals(single.getPlugName())).filter(single -> !single.getFormId().equals(formId)).findAny();
              return !optionalIFormPlug.isPresent();
          }
          return true;
        }).map(plug->RelationDict.builder().key(plug.plugName()).value(plug.plugDesc()).build())
                .collect(Collectors.toList());
    }

}
